
#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#include <inttypes.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/signal.h>
/*UART*/
#define F_OSC 16000000
#define UART_BAUD_RATE 9600
#define UART_BAUD_CALC(UART_BAUD_RATE,F_OSC) ((F_OSC)/((UART_BAUD_RATE)*16l)-1)
unsigned char i=0xff;

unsigned char data;

/*****************************************************************************
Name:          SIGNAL
Description:    USART RX interrupt
            INTERRUPT can be interrupted
            SIGNAL can't be interrupted
            (USART_RXC_vect)      
            (SIG_UART_RECV)
Parameters:    SIG_UART_RECV
Returns:        SIGNAL
*****************************************************************************/
ISR (USART_RXC_vect)
{ 
  data = UDR;
  
  
  USART_Transmit(data);

}


/*****************************************************************************
Name:          USART_Transmit_s
Description:    Transmits a string over USART
Parameters:    *s
Returns:        none
*****************************************************************************/
void USART_Transmit_s( char *s )
{
  //  loop until *s != NULL
  while (*s)
      {
        USART_Transmit(*s);
        s++;
      }
}


/*****************************************************************************
Name:          USART_Receive
Description:    Receive character from USART
Parameters:    void
Returns:        UDR
*****************************************************************************/
unsigned char USART_Receive( void )
{
  /* Wait for data to be received */
  while ( !(UCSRA & (1<<RXC)) )
      { };
  /* Get and return received data from buffer */
  return UDR;
}


/*****************************************************************************
Name:          USART_Transmit
Description:    Transmits a character over USART
Parameters:    data
Returns:        none
*****************************************************************************/
void USART_Transmit( unsigned char data )
{
  /* Wait for empty transmit buffer */
  while ( !( UCSRA & (1<<UDRE)) );
    
  /* Put data into buffer, sends the data */
  UDR = data;
}


/*****************************************************************************
Name:          UART_init
Description:    Inisialize UART
Parameters:    void
Returns:        none
*****************************************************************************/
void USART_init(void)
{
  // set baud rate
  UBRRH = (uint8_t)(UART_BAUD_CALC(UART_BAUD_RATE,F_OSC)>>8);
  UBRRL = (uint8_t)UART_BAUD_CALC(UART_BAUD_RATE,F_OSC);
  // Enable receiver and transmitter; enable RX interrupt
  UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
  //asynchronous 8N1
  UCSRC = (1<<URSEL)|(3<<UCSZ0);

  }


/*****************************************************************************
Name:          Delay
Description:    Delay program for ...ms
Parameters:    ms
Returns:        none
*****************************************************************************/
void Delay(int ms)
{
  int i;

  for (i=0;i<ms;i++)
      _delay_ms(1);
}

/*******************************LCD RUTINE!!!!***************************/
         /**< clock frequency in Hz, used to calculate delay timer */

/** 
*  @name  Definitions for Display Size 
*  Change these definitions to adapt setting to your display
*/
void interrupt_init(void){
   
    GICR = 0;
    GIFR |= 1<<INTF0;
    GICR |= (1<<INT0);
    SREG |= 0x80;

}

 ISR(INT0_vect)
    {

	USART_Transmit(i/=2);
	PORTD = 0x00;
	_delay_ms(100);
	PORTC = 0xff;
	_delay_ms(100);        
		
		
    }
	
int main(void){



	//unsigned char i=0;
	USART_init();
	
	interrupt_init();
	unsigned char c0=0x66,c1=0x66,c2=0x66,c3,c4,c5,c6,c7,c8,c9;
	unsigned char c10,c11,c12,c13,c14,c15,c16,c17,c18,c19;
	unsigned char c20,c21,c22,c23,c24,c25,c26,c27,c28,c29,c30,c31;
	DDRB = 0x00;
	PORTB = 0xff;
	
	DDRC = 0xff;
	PORTC=0x00;
	_delay_ms(1000);
	_delay_ms(1000);
	_delay_ms(1000);
	_delay_ms(1000);
	while(1){
		
	
		
		
	}

}